﻿using System;
using System.Security.Principal;
using DomainModel;
using Repository.Abstract;
using Services.Abstract;

namespace Services.Concrete
{
    public class UserProfileService : Service<UserProfile>, IUserProfileService
    {
        private readonly IUserProfileRepository _userProfileRepository;

        private const string DefaultPhotoUrl = "/Uploads/Avatars/_noavatar.gif";

        public UserProfileService(IUserProfileRepository userProfileRepository, IPrincipal currentUser, IPermissionService permissionService)
            : base(userProfileRepository, currentUser, permissionService)
        {
            _userProfileRepository = userProfileRepository;
        }

        public new void SaveOrUpdate(UserProfile profile)
        {
            if (profile == null)
                throw new ArgumentNullException("profile");

            if (profile.Id > 0)
            {
                if (!PermissionService.CanChangeProfile(profile.User))
                    throw new Exception("Недостаточно прав чтобы измянять профиль.");
            }

            if (string.IsNullOrEmpty(profile.PhotoUrl))
            {
                profile.PhotoUrl = DefaultPhotoUrl;
            }

            base.SaveOrUpdate(profile);
        }

        public bool NoProfilePhoto(UserProfile userProfile)
        {
            return userProfile.PhotoUrl == DefaultPhotoUrl;
        }
    }
}